package com.panopset.games.playingcards;

import java.util.List;
import java.util.ArrayList;

import com.panopset.centralen.util.MathUtil;

/**
 * Single shuffled deck of 52 cards.
 * @author Karl Dinwiddie
 *
 */
public final class Deck {

    /**
     * Shuffled cards.
     */
    private List<Card> shuffled = new ArrayList<Card>();

    /**
     * Cards are shuffled in the constructor.
     */
    public Deck() {
        shuffle();
    }

    /**
     * @Shuffle cards.
     */
    private void shuffle() {
        List<Card> cards = new ArrayList<Card>();
        for (Card card : Card.values()) {
            cards.add(card);
        }
        while (!cards.isEmpty()) {
            shuffled.add(cards.remove(MathUtil.random(0, cards.size() - 1)));
        }
    }

    /**
     * @return Number of cards remaining in the deck.
     */
    public int remaining() {
        return shuffled.size();
    }

    /**
     * Deal a card.
     * @return Dealt card (removed from deck).
     */
    public Card deal() {
        if (shuffled.size() < 1) {
            throw new ArrayIndexOutOfBoundsException();
        }
        return shuffled.remove(0);
    }
}
